<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cate;
use App\Models\Article;
use Auth;

class ArticleController extends Controller
{
    protected $model_article;
    protected $model_cate;
    protected $page;

    public function __construct(Cate $cate, Article $article)
    {
        $this->model_article = $article;
        $this->model_cate = $cate;
        $this->page = 15;
    }
    public function index()
    {
        $articles = $this->model_article->getList($this->page, ['user_id' => Auth::id()]);
        return view('admin.article.article_list', ['articles' => $articles]);
    }

    public function add()
    {
        $cates = $this->model_cate->getList(0);
        return view('admin.article.article_add', ['cates' => $cates]);
    }

    public function addSave(Request $request)
    {
        $messages = [
            'required' => ':attribute 必须填写。',
        ];
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
        ],$messages);
        if (empty($request->cates)) {
            $request->cates_str = '';
            $request->cates_id = '';
        } else {
            $request->cates_str = $this->model_cate->getCateSerialize($request->cates);
            $request->cates_id = ','.implode(',', $request->cates).',';
        }
        $request->user_name = Auth::user()->name;
        $request->user_id = Auth::id();
        $result = $this->model_article->create($request);
        if ($result) {
            return redirect()->route('admin.article.list');
        } else {
//            TODO
            dd('创建文章失败');
        }
    }

    public function edit($id)
    {
        $article_info = $this->model_article->getInfo($id);
        $article_info->cate_ids = explode(',', $article_info->cate_id);
        $cates = $this->model_cate->getList(0);
        return view('admin.article.article_edit', ['cates' => $cates, 'article_info' => $article_info]);
    }

    public function editSave(Request $request)
    {
        $messages = [
            'required' => ':attribute 必须填写。',
        ];
        $this->validate($request,
            [
            'title' => 'required',
            'content' => 'required',
            ],
            $messages
        );
        if (empty($request->cates)) {
            $request->cates_str = '';
            $request->cates_id = '';
        } else {
            $request->cates_str = $this->model_cate->getCateSerialize($request->cates);
            $request->cates_id = ','.implode(',', $request->cates).',';
        }
        $result = $this->model_article->edit($request);
        if ($result) {
            return redirect()->route('admin.article.list');
        } else {
//            TODO
            dd('更新文章失败');
        }
    }

    public function del($id)
    {
        $result = $this->model_article->del($id);
        if ($result) {
            return redirect()->route('admin.article.list');
        } else {
//            TODO
            dd('删除文章失败');
        }
    }
}
